package com.wss.merchant.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wss.merchant.domain.CommonResult;
import com.wss.merchant.domain.DlNewUserGrouper;
import com.wss.merchant.domain.DlUser;
import com.wss.merchant.service.DlNewCommunityService;
import com.wss.merchant.service.DlNewUserGrouperService;
import com.wss.merchant.service.DlUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 社区表 前端控制器
 * </p>
 *
 * @author wss
 * @since 2021-03-08
 */
@Controller
@RequestMapping("/grouper")
public class DlNewUserGrouperController {
    @Autowired
    private DlNewUserGrouperService dlNewUserGrouperService;
    @Autowired
    private DlUserService dlUserService;
    @Autowired
    private DlNewCommunityService dlNewCommunityService;



    //查询
    @RequestMapping("/getGrouper")
    @ResponseBody
    public CommonResult getGrouper(@RequestBody Map<String,String> map){
        Page<Map> page = new Page<>(Integer.parseInt(map.get("current")),Integer.parseInt(map.get("size")));
        Page<Map> grouper = dlNewUserGrouperService.getGrouper(page, map);
        return  new CommonResult(200,"查询成功",grouper);
    }
    //修改
    @RequestMapping("/alter")
    @ResponseBody
    public CommonResult alter(@RequestBody Map<String,Integer> map){
        DlNewUserGrouper id = dlNewUserGrouperService.getById(map.get("id"));
        Integer status = id.getStatus();
        if(status == 1){
            id.setStatus(0);
            dlNewUserGrouperService.saveOrUpdate(id);
        }else if(status == 0){
            id.setStatus(1);
            dlNewUserGrouperService.saveOrUpdate(id);
        }
        return new CommonResult(200,"成功");
    }

    //添加团长
    @RequestMapping("/addGrouper")
    @ResponseBody
    public CommonResult addGrouper(@RequestBody Map<String,String> map){

        QueryWrapper queryWrapper = new QueryWrapper();
        String s = map.get("mobile");
        queryWrapper.eq("mobile",s);
        Integer uid = dlUserService.getOne(queryWrapper).getId();


        QueryWrapper queryWrapper2 = new QueryWrapper();
        queryWrapper2.eq("name", map.get("community"));
        Integer cid = dlNewCommunityService.getOne(queryWrapper2).getId();


        DlNewUserGrouper dlNewUserGrouper = new DlNewUserGrouper();
        dlNewUserGrouper.setUserId(uid);
        dlNewUserGrouper.setCid(cid);
        dlNewUserGrouper.setName(map.get("name"));
        dlNewUserGrouper.setStatus(1);
        dlNewUserGrouperService.saveOrUpdate(dlNewUserGrouper);
        return new CommonResult(200,"成功");
    }



}

